package com.huaao.micropolice.core.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.huaao.micropolice.core.common.page.Page;
import com.huaao.micropolice.core.common.page.Sort;
import com.huaao.micropolice.core.model.system.Button;
import com.huaao.micropolice.core.model.system.Menu;
import com.huaao.micropolice.core.repository.system.ButtonDao;
import com.huaao.micropolice.core.repository.system.MenuDao;
import com.huaao.micropolice.core.service.system.ButtonService;

@Service
public class ButtonServiceImpl implements ButtonService{
	
	@Autowired
	private ButtonDao buttonDao;
	
	@Autowired
	private MenuDao menuDao;
	
	@Override
	public void list(Sort sort, Page page) {
		buttonDao.listByMenu(sort,page);
	}

	@Override
	@Transactional
	public void dels(String ...ids) {
		buttonDao.deleteById(ids);
	}

	@Override
	@Transactional
	public void add(Button button) {
		Menu menu = menuDao.load(button.getMenu().getId());
		button.setMenu(menu);
		buttonDao.save(button);
	}

	@Override
	public Button load(String id) {
		return buttonDao.load(id);
	}

	@Override
	@Transactional
	public void edit(Button button) {
		Menu menu = menuDao.load(button.getMenu().getId());
		Button old = buttonDao.load(button.getId());
		old.setName(button.getName());
		old.setPermission(button.getPermission());
		old.setMenu(menu);
		buttonDao.update(old);
	}

	@Override
	public List<Button> findByMenuId(String menuId) {
		return buttonDao.findByMenuId(menuId);
	}
}
